package top.mhtools.pigeon.ding.service.support;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.mhtools.pigeon.common.enums.DingMessageTypeEnum;
import top.mhtools.pigeon.common.persistence.DingRequest;
import top.mhtools.pigeon.ding.dto.DingMessageReqDTO;

import java.util.List;
import java.util.Objects;

/**
 * 文本消息发送器
 *
 * @author machen
 * @date 2020/4/24 19:34
 */
@Slf4j
@Component
public class DingMessageTextSolver extends AbstractDingMessageExecuteStrategy {

    @Override
    public void execute(DingRequest dingRequest) {
        String serverUrl = dingtalkUrl + "?access_token=" + dingRequest.getToken();
        // 指定消息类型
        DingTalkClient client = new DefaultDingTalkClient(serverUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype(DingMessageTypeEnum.TEXT.getName());

        // 指定消息体
        DingMessageReqDTO.TextDTO textDTO = dingRequest.getTextDTO();

        OapiRobotSendRequest.Text test = new OapiRobotSendRequest.Text();
        test.setContent(textDTO.getContent());
        request.setText(test);

        List<String> atList = dingRequest.getAtMobiles();
        if (!Objects.isNull(atList)) {
            request.setAt(buildAt(atList));
        }
        sendMessage(client, request);
    }

    @Override
    public String supports() {
        return DingMessageTypeEnum.TEXT.getName();
    }
}
